##!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json


class man(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age


d = man('tianyi', 29)


def man2dict(man):
    return {'name': man.name, 'age': man.age}


def dict2man(dict):
    return man(dict['name'], dict['age'])


with open('C:\\Users\\liuti\\Documents\\a', 'w') as f:
    json.dump(d, f, default=man2dict)

with open('C:\\Users\\liuti\\Documents\\a', 'r') as f:
    m = json.load(f, object_hook=dict2man)
    print(type(m), ' ', m.name, ' ', m.age)

obj = dict(name='小明', age=20)
s = json.dumps(obj, ensure_ascii=True)
print(s)
s = json.dumps(obj, ensure_ascii=False)
print(s)
